import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';
import { sign, verify } from "jsonwebtoken";
import { jwtConstants } from './constants';

@Injectable()
export class AuthService {
    async signIn(obj: any) {
        const token = sign(obj, jwtConstants.secret);
        return token;
    }

    verifyAsync(token: string) {
        return new Promise(resolve => {
            verify(token, jwtConstants.secret, (err, res) => {
                if (err) {
                    console.log("res === ", res);
                    throw new HttpException('This token is not valid', HttpStatus.NON_AUTHORITATIVE_INFORMATION);
                }
                resolve(res);
            });
        })

    }

    create(createAuthDto: CreateAuthDto) {
        return 'This action adds a new auth';
    }

    findAll() {
        return `This action returns all auth`;
    }

    findOne(id: number) {
        return `This action returns a #${id} auth`;
    }

    update(id: number, updateAuthDto: UpdateAuthDto) {
        return `This action updates a #${id} auth`;
    }

    remove(id: number) {
        return `This action removes a #${id} auth`;
    }
}
